
from django.views.generic import View
from django.core.cache import cache
from django_redis import get_redis_connection
from django.shortcuts import render,redirect
from django.http import JsonResponse
from django.urls import reverse
from order.models import OrderGoods
from django.core.paginator import Paginator
from goods.models import *
# Create your views here.
class add_to_cart(View):
    def post(self,request):
        user=request.user
        if user.is_authenticated:
            data=request.POST
            count=data.get('count')
            sku_id=data.get('sku_id')
            try:
                count=int(count)
                if count>0:
                    cart_user="cart_{}".format(user.id)
                    con=get_redis_connection('default')
                    cart_count=con.hget(cart_user,sku_id)
                    if cart_count:
                        count+=int(cart_count)
                    con.hset(cart_user,sku_id,count)
                    cart_num=con.hlen(cart_user)
                    return JsonResponse({'res':5,'msg':'success','cart_num':cart_num})
            except Exception as e:
                print(e)
        return JsonResponse({'res':0,'msg':'添加失败'})
class cart_View(View):
    def get(self,request):
        user=request.user
        if user.is_authenticated:
            cart_user="cart_{}".format(user.id)
            con=get_redis_connection('default')
            cart_all=con.hgetall(cart_user)
            print(cart_all,len(cart_all))
            cart_temp={}
            for good,num in cart_all.items():
                good_sku=GoodsSKU.objects.get(id=good)
                cart_temp[good]={'good_sku':good_sku,'num':num}
            
            content={
                'cart_all':cart_temp,
            }
            print(cart_temp)
            return render(request,'cart.html',content)
        return redirect(reverse("user:login"))
class cart_update_view(View):
    def post(self,request):
        count=request.POST.get('count')
        sku_id=request.POST.get('sku_id')
        print(count,sku_id)
        user=request.user
        if user.is_authenticated:

            try:
                count=int(count)
                if count>=0:
                    cart_user="cart_{}".format(user.id)
                    con=get_redis_connection('default')
                    con.hset(cart_user,sku_id,count)
                   
                    return JsonResponse({'res':5,'msg':'success'})
            except Exception as e:
                print(e)
        return JsonResponse({'res':0,'msg':'添加失败'})
class cart_delete_view(View):
    def post(self,request):
        
        sku_id=request.POST.get('sku_id')
       
        user=request.user
        if user.is_authenticated:

            try:
                
                
                    cart_user="cart_{}".format(user.id)
                    con=get_redis_connection('default')
                    num=con.hdel(cart_user,sku_id)
                    if num:
                        return JsonResponse({'res':5,'msg':'success'})
                    else:
                        raise("购物车中没有该商品")
            except Exception as e:
                print(e)
        return JsonResponse({'res':0,'msg':'添加失败'})




